package com.workcell.workdata.im.component.validator;

import com.workcell.workdata.im.annotation.validator.MessageVoiceFormat;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.model.message.ChatVoice;
import com.workcell.workdata.im.model.request.MessageRequest;
import com.workcell.workdata.im.util.JSON;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class MessageVoiceValidator implements ConstraintValidator<MessageVoiceFormat, MessageRequest> {

    @Override
    public boolean isValid(MessageRequest momentRequest, ConstraintValidatorContext context) {

        if (momentRequest.getFormat() != MessageFormat.VOICE){
            return true;
        }

        ChatVoice voice = JSON.parseNullable(momentRequest.getContent(), ChatVoice.class);

        return voice != null
                && voice.getFile() != null
                && voice.getLength() != 0;
    }
}
